package com.success.application.user.activity;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.reflect.TypeToken;
import com.success.application.api.YuanbaoApi;
import com.success.application.base.BaseDetailActivity;
import com.success.application.common.param.Params;
import com.success.application.common.result.Result;
import com.success.application.myapplication.MainActivity;
import com.success.application.myapplication.R;
import com.success.application.user.AccountHelper;
import com.success.application.user.bean.User;
import com.success.application.util.DialogHelper;
import com.success.application.widget.SimplexToast;
import com.success.application.widget.TDevice;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by WYN on 2017/2/7.
 */

public class LoginActivity extends BaseDetailActivity<Map> {

    private Dialog dialog;
    private static final int REQUEST_CODE_FORGETPWD= 1;
    private static final int REQUEST_CODE_REGISTER= 2;

    @Bind(R.id.ll_login_username)
    LinearLayout mLlLoginUsername;//用户名LinearLayout
    @Bind(R.id.et_login_username)
    EditText mEtLoginUsername;//用户名
    @Bind(R.id.iv_login_username_icon)
    ImageView mlogin_username_icon;//

    @Bind(R.id.ll_login_pwd)
    LinearLayout mLlLoginPwd;
    @Bind(R.id.et_login_pwd)
    EditText mEtLoginPwd;
    @Bind(R.id.iv_login_pwd_icon)
    ImageView mlogin_pwd_icon;


//    @Bind(R.id.tv_login_forget_pwd)
//    TextView mTvLoginForgetPwd;

    @Bind(R.id.bt_login_register)
    TextView mTvLoginRegister;

    @Bind(R.id.bt_login_submit)
    Button mBtLoginSubmit;

    @Override
    protected Type getType() {
        return new TypeToken<Result<Map>>() {
        }.getType();
    }
    // 清除键
//    @OnClick(R.id.iv_login_sjh_clear)
//    void clearLoginSjh(){
//        mEtLoginUsername.setText("");
//    }
//    @OnClick(R.id.iv_login_pwd_clear)
//    void clearLoginPwd(){
//        mEtLoginPwd.setText("");
//    }
    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_main_login;
    }


    @Override
    protected boolean initBundle(Bundle bundle) {
        super.initBundle(bundle);
        return true;
    }

    @Override
    protected void initWidget() {
        super.initWidget();
    }

    @Override
    protected void initData() {
        super.initData();
    }

    protected int getTitleRes() {
        return R.string.main_login_user;
    }

//    protected int getLeftIconRes() {
//        return R.drawable.selector_nav_back_light;
//    }

    @Override
    protected View.OnClickListener getLeftIconClickListener() {
        return new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        };
    }

    /**
     * show the login activity
     *
     * @param context context
     */
    public static void show(Context context) {
        Intent intent = new Intent(context, LoginActivity.class);
        context.startActivity(intent);
    }

//    @OnClick(R.id.et_login_username)
//    void onClickLoginUserName() {
//        mEtLoginPwd.clearFocus();
//        mEtLoginUsername.setFocusableInTouchMode(true);
//        mEtLoginUsername.requestFocus();
//    }
//
//    @OnClick(R.id.et_login_pwd)
//    void onClickLoginPwd() {
//        mEtLoginUsername.clearFocus();
//        mEtLoginPwd.setFocusableInTouchMode(true);
//        mEtLoginPwd.requestFocus();
//    }

//    /**
//     * 忘记密码
//     */
//    @OnClick(R.id.tv_login_forget_pwd)
//    void onClickLoginForgetPwd() {
//        Intent intent = new Intent(getApplicationContext(), ForgetPasswordActivity.class);
//        startActivityForResult(intent, REQUEST_CODE_FORGETPWD);
//    }

    /**
     * 立即注册
     */
    @OnClick(R.id.bt_login_register)
    void onClickLoginRegister() {
        Intent intent = new Intent(getApplicationContext(), RegisterActivity.class);
        startActivityForResult(intent, REQUEST_CODE_REGISTER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == REQUEST_CODE_FORGETPWD){
            if (resultCode == RESULT_OK) {
                finish();
            }
        }
        if(requestCode == REQUEST_CODE_REGISTER){
            if (resultCode == RESULT_OK) {
                String isAccount = data.getStringExtra("isAccount");
                if(TextUtils.isEmpty(isAccount)){
                    finish();
                }
            }
        }
    }

    /**
     * 用户登录
     */
    @OnClick(R.id.bt_login_submit)
    void onClickLoginSubmit() {
        loginRequest();
        dialog = DialogHelper.getProgressDialog(LoginActivity.this, "登录中");
        dialog.show();
    }

    private void loginRequest() {
        String tempUsername = mEtLoginUsername.getText().toString().trim();
        String tempPwd = mEtLoginPwd.getText().toString().trim();
        if (!TextUtils.isEmpty(tempPwd) && !TextUtils.isEmpty(tempUsername)) {
            //登录成功,请求数据进入用户个人中心页面
            if (TDevice.hasInternet()) {
                requestLogin(tempUsername, tempPwd);
            } else {
                SimplexToast.show(getApplicationContext(), "网络错误");
            }
        } else {
            SimplexToast.show(getApplicationContext(), "请输入正确的手机号");
        }
    }

    private void requestLogin(String tempUsername, String tempPwd) {
            Params params = new Params();
            params.put("User_yhm", tempUsername);
            params.put("User_mm", tempPwd);
        try {
            YuanbaoApi.getUserLogin(params, mHandler);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //微信登陆
//    @OnClick(R.id.iv_weixin_login)
//    void weixinLogin(){
////        //判断哪个页面到微信授权
//        WXEntryActivity.activityType = "LoginActivity";
//        if (!YuanbaoApplication.api.isWXAppInstalled()) {
//            Toast.makeText(getApplicationContext(), "请安装微信", Toast.LENGTH_SHORT).show();
//            return;
//        }
//        final SendAuth.Req req = new SendAuth.Req();
//        req.scope = "snsapi_userinfo";
//        req.state = "diandi_wx_login";
//        YuanbaoApplication.api.sendReq(req);
//        finish();
//    }
    @Override
    protected void setData(Result<Map> result) {
        if (dialog != null) {
            dialog.dismiss();
        }
        if(result.getMsg()=="用户名或密码错误!"){
            SimplexToast.show(getApplicationContext(), result.getMsg());
        }
        //获取用户登录成功之后的信息
        Map mMap = result.getData();

        User user = new User();
        //系统用户的字段
        if(mMap.get("id")!=null){
            user.setId(mMap.get("id").toString());
        }
        if(mMap.get("User_yhm")!=null){
            user.setUser_yhm(mMap.get("User_yhm").toString());
        }
        if(mMap.get("User_mm")!=null){
            user.setUser_mm(mMap.get("User_mm").toString());
        }
        if(mMap.get("User_nc")!=null){
            user.setUser_nc(mMap.get("User_nc").toString());
        }
        if(mMap.get("User_dh")!=null){
            user.setUser_dh(mMap.get("User_dh").toString());
        }
        if(mMap.get("User_qq")!=null){
            user.setUser_qq(mMap.get("User_qq").toString());
        }
        if(mMap.get("User_nl")!=null){
            user.setUser_nl(mMap.get("User_nl").toString());
        }
        if(mMap.get("User_email")!=null){
            user.setUser_email(mMap.get("User_email").toString());
        }
        if(mMap.get("User_xx")!=null){
            user.setUser_xx(mMap.get("User_xx").toString());
        }
        if(mMap.get("User_tx")!=null){
            user.setUser_tx(mMap.get("User_tx").toString());
        }
        if(mMap.get("User_xymc")!=null){
            user.setUser_xymc(mMap.get("User_xymc").toString());
        }
        if(mMap.get("User_xmc")!=null){
            user.setUser_xmc(mMap.get("User_xmc").toString());
        }
        if(mMap.get("User_bjmc")!=null){
            user.setUser_bjmc(mMap.get("User_bjmc").toString());
        }
        if(mMap.get("User_js")!=null){
            user.setUser_js(mMap.get("User_js").toString());
        }
        if(mMap.get("User_djrq")!=null){
            user.setUser_djrq(mMap.get("User_djrq").toString());
        }
        if(mMap.get("User_bz")!=null){
            user.setUser_bz(mMap.get("User_bz").toString());
        }
        if(mMap.get("User_zt")!=null){
            user.setUser_zt(mMap.get("User_zt").toString());
        }
        if(mMap.get("User_csnyr")!=null){
            user.setUser_csnyr(mMap.get("User_csnyr").toString());
        }
        if(mMap.get("User_role")!=null){
            user.setUser_role(mMap.get("User_role").toString());
        }
        AccountHelper.updateUserCache(user);
        Intent getIntent = getIntent();
        String login = getIntent.getStringExtra("loginactivity");
        if(login != null){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }else{
            setResult(RESULT_OK, getIntent);
            finish();
        }
    }
}
